﻿using UnityEngine;


//单纯模拟重力和阻力
public class MonsterGravitySimulation : MonoBehaviour
{
    [SerializeField] private float gravity;
    [SerializeField] private float drag;
    private float currDrag = 0;
    private Monster monster;

    private void Start()
    {
        monster = transform.GetComponent<Monster>();
    }


    //模拟重力和阻力
    private void Update()
    {
        //if (!monster.IsGround)
        //{
        //    monster.rigidBody2D.velocity = new Vector2(monster.rigidBody2D.velocity.x, monster.rigidBody2D.velocity.y - Time.deltaTime * gravity);
        //}

        if (!monster.IsGround)
        {
            monster.transform.Translate(Vector3.down * Time.deltaTime * gravity, Space.World);
        }
 
        //与velocity X 轴相反方向加力，从而实现阻力效果
        //if(monster.rigidBody2D.velocity.x < 0)
        //{
        //    //阻力消去了所有惯性的情况
        //    if(monster.rigidBody2D.velocity.x + Time.deltaTime * drag > 0)
        //    {
        //        monster.rigidBody2D.velocity = new Vector2(0.0f, monster.rigidBody2D.velocity.y);
        //    }
        //    else
        //    {
        //        monster.rigidBody2D.velocity += Vector2.right * Time.deltaTime * drag;
        //    }
        //}
        //else if (monster.rigidBody2D.velocity.x > 0)
        //{
        //    if (monster.rigidBody2D.velocity.x - Time.deltaTime * drag < 0)
        //    {
        //        monster.rigidBody2D.velocity = new Vector2(0.0f, monster.rigidBody2D.velocity.y);
        //    }
        //    else
        //    {
        //        monster.rigidBody2D.velocity += Vector2.left * Time.deltaTime * drag;
        //    }
        //}
    }
}
